package com.ljc.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ljc.dto.Result;
import com.ljc.entity.ProjectPlanning;
import com.ljc.entity.ProjectPlanningReview;
import com.ljc.service.ProjectPlanningService;
import com.ljc.util.JWTUtil;
import com.ljc.vo.AssignExpert;
import com.ljc.vo.ProjectPlanningReviewSearch;
import com.ljc.vo.ProjectPlanningSearch;
import com.sun.org.apache.regexp.internal.RE;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
@ResponseBody
@CrossOrigin
@Slf4j
@RequestMapping("projectPlanning")
public class ProjectPlanningController {
    @Autowired
    private ProjectPlanningService projectPlanningService;

    @RequestMapping("addProjectPlanning")
    public Result addProjectPlanning(HttpServletRequest request, @RequestBody ProjectPlanning projectPlanning){
        log.info("add  project Planning:[{}]", projectPlanning);

        Result result = new Result();
        String token = request.getHeader("accessToken");
        String id = JWTUtil.getTokenInfo(token).getClaim("id").asString();
        projectPlanning.setUserId(Integer.valueOf(id));
        try {
            projectPlanningService.addProjectPlanning(projectPlanning);
            result.setStatus(1).setMsg("提交成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setStatus(0).setMsg("增加失败");
        }
        return result;
    }
    @RequestMapping("deleteById")
    public Result deleteById(@RequestParam int id){
        Result result = new Result();
        projectPlanningService.deleteById(id);
        result.setStatus(1).setMsg("删除成功");
        return result;
    }
    @RequestMapping("getProjectPlanningByUserId")  //quit
    public Result getProjectPlanningByUserId(HttpServletRequest request){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        String id = JWTUtil.getTokenInfo(token).getClaim("id").asString();
        List<ProjectPlanning> projectPlannings = projectPlanningService.getByUserId(Integer.valueOf(id));
        result.setStatus(1).setMsg("获取成功").setData(projectPlannings);
        for (ProjectPlanning projectPlanning : projectPlannings) {
            System.out.println(projectPlanning);
        }
        return result;
    }

    @RequestMapping("searchByCollegeUser")
    public Result searchByCollegeUser(HttpServletRequest request, @RequestBody ProjectPlanningSearch search){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        String id = JWTUtil.getTokenInfo(token).getClaim("id").asString();
       // List<PurchasePlanning> purchasePlannings = purchasePlanningService.getByUserId(Integer.valueOf(id));
        search.setUserId(Integer.valueOf(id));
        if(search.getPageNum()!=-1){
            PageHelper.startPage(search.getPageNum(),search.getPageSize());
            List<ProjectPlanning> projectPlannings = projectPlanningService.searchByCollegeUser(search);
            PageInfo<ProjectPlanning> projectPlanningPageInfo = new PageInfo<>(projectPlannings);
            result.setStatus(1).setMsg("获取成功").setData(projectPlanningPageInfo);
        }else{
            List<ProjectPlanning> projectPlannings1 = projectPlanningService.searchByCollegeUser(search);
            result.setStatus(1).setMsg("获取成功").setData(projectPlannings1);
        }
        return result;
    }
    @RequestMapping("getById")
    public Result getById(@RequestParam int id){
        Result result = new Result();
        ProjectPlanning projectPlanning = projectPlanningService.getById(id);
        result.setStatus(1).setMsg("查询成功").setData(projectPlanning);
        return result;
    }
    @RequestMapping("searchByAdministrator")
    public Result searchByAdministrator(HttpServletRequest request, @RequestBody ProjectPlanningReviewSearch search){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        String id = JWTUtil.getTokenInfo(token).getClaim("id").asString();
        search.setDeanId(Integer.valueOf(id));
        if (search.getPageNum()==-1){
            List<ProjectPlanning> projectPlannings = projectPlanningService.searchByAdministrator(search);
            result.setStatus(1).setMsg("获取成功").setData(projectPlannings);
        }else {
            PageHelper.startPage(search.getPageNum(),search.getPageSize());
            List<ProjectPlanning> projectPlannings = projectPlanningService.searchByAdministrator(search);
            PageInfo<ProjectPlanning> projectPlanningPageInfo = new PageInfo<>(projectPlannings);
            result.setStatus(1).setMsg("分页获取成功").setData(projectPlanningPageInfo);
        }
        return result;
    }
    @RequestMapping("projectPlanningReview")
    public Result projectPlanningReview(HttpServletRequest request, @RequestBody ProjectPlanningReview review){
        Result result = new Result();
        review.setDate(new Date());
        try {
            projectPlanningService.review(review);
            result.setStatus(1).setMsg("审核成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setStatus(0).setMsg("审核失败");
        }
        return result;
    }
    @RequestMapping("setStatus")
    public Result setStatus(@RequestParam int id,@RequestParam int status){
        Result result = new Result();
        try {
            projectPlanningService.setStatus(id,status);
            result.setStatus(1).setMsg("操作成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setStatus(0).setMsg("操作成功");
        }
        return result;
    }
    @RequestMapping("assignExpert")
    public Result assignExpert(@RequestBody List<AssignExpert> assignExperts){
        Result result = new Result();
        try {
                projectPlanningService.assignExpert(assignExperts);
                result.setStatus(1).setMsg("操作成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setStatus(0).setMsg("失败");
        }
        return result;
    }
    @RequestMapping("searchByExpertUser")
    public Result searchByExpertUser(HttpServletRequest request,@RequestBody ProjectPlanningSearch search){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        String id = JWTUtil.getTokenInfo(token).getClaim("id").asString();
       search.setExpertId(Integer.valueOf(id));
        if(search.getPageNum()!=-1){
            PageHelper.startPage(search.getPageNum(),search.getPageSize());
            List<ProjectPlanning> projectPlannings = projectPlanningService.searchByExpertUser(search);
            PageInfo<ProjectPlanning> projectPlanningPageInfo = new PageInfo<>(projectPlannings);
            result.setStatus(1).setMsg("获取成功").setData(projectPlanningPageInfo);
        }else{
            List<ProjectPlanning> projectPlannings1 = projectPlanningService.searchByExpertUser(search);
            result.setStatus(1).setMsg("获取成功").setData(projectPlannings1);
        }
        return result;
    }
    @RequestMapping("update")
    public Result updateProjectPlanning(@RequestBody ProjectPlanning projectPlanning){
        Result result = new Result();
        try {
            projectPlanningService.updateProjectPlanning(projectPlanning);
            result.setStatus(1).setMsg("保存成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setStatus(1).setMsg("失败");
        }
        return result;
    }
}
